import queue
import threading
import time

from crawer.download import Download
from crawer.parser import ParserManager


class Manager(threading.Thread):
    def __init__(self):
        self.queue = queue.Queue()
        self.file = Download(self)
        self.parser = ParserManager(self)
        self.urls = queue.Queue()
        # self.file = Download(manager)
        threading.Thread.__init__(self)
        pass

    def joinBoot(self, boot):
        for url in boot:
            # print(url.split('/')[2].split('.')[1])
            self.add_url((url.split('/')[2].split('.')[1], 'first', url))

    def run(self):
        """开启下载线程"""
        # file = threading.Thread(target=self.file.down_file())
        self.file.start()

        """开启页面解析线程"""
        self.parser.start()
        # file.join()
        # self.parser.join()
        while True:
            print("main")
            time.sleep(1)
            # pass
            # try:
            #     self.manager

    def add_url(self, item):
        print(item)
        self.urls.put(item)

    def get_url(self):
        return self.urls.get()

    def add_parser(self, item):
        self.queue.put(item)

    def get_parser(self):
        return self.queue.get()

    def url_empty(self):
        return self.urls.empty()

    def parser_empty(self):
        return self.queue.empty()


